package com.xxl.services.impl;

import com.xxl.bean.Classes;
import com.xxl.dao.ClassesDao;
import com.xxl.dao.impl.ClassesDaoImpl;
import com.xxl.services.ClassesService;
import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class ClassesServiceImpl implements ClassesService {
    ClassesDao classesDao = new ClassesDaoImpl();
    @Override
    public void addOrUpdate(String classesId,String classesName) throws IOException {
        if (classesId.length()>0){
            try {
                classesAddOrUpdate(classesId, classesName);
            } catch (IOException | ClassNotFoundException e) {
                System.out.println(e);
            } catch (SQLException throwables) {
                System.out.println(throwables);
            }
        }
    }
    private void classesAddOrUpdate(String classesId, String classesName) throws IOException, SQLException, ClassNotFoundException {
        ResultSet rs = classesDao.selByClassesId(Integer.parseInt(classesId));
        try {
            if (rs.next()){
                classesDao.update(Integer.parseInt(classesId), classesName);
            }else {
                classesDao.add(Integer.parseInt(classesId), classesName);
            }
        }catch (SQLException throwables) {
            System.out.println(throwables);
        }
    }

    @Override
    public boolean delByClassesId(int parseInt) {
        return classesDao.delByClassesId(parseInt);
    }

    @Override
    public void listAddClasses(List list) throws SQLException {
        ResultSet classes = classesDao.getAll();
        while (true){
            try {
                if (!classes.next()){
                    break;
                }
            }finally {
                System.out.println("ClassesServlet.listAddClasses");
            }
            try {
                Classes clazz = new Classes(classes.getInt("classesId"), classes.getString("classesName"));
                list.add(clazz);
            } finally {
                System.out.println("ClassesServlet.listAddClasses");
            }
        }
    }


}
